/**
 * Gets the login form from the server and displays it.
 */
function getLoginArea() {
	$('userAreaHeader').update( 'Logi sisse' ).show();
	
	new Ajax.Request( siteUrl + 'users.php?action=getLoginArea', {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('userArea').update( response.responseText ).show();
			}
		},
		onFailure: function( response ) {
			$('userArea').update( 'Unknown error.' ).show();
		}
	});
}

function loadAllUsers(){
	new Ajax.Request( siteUrl + 'modules.php?action=loadAllUsers', {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('centerArea').update( response.responseText);
			}
		},
		onFailure: function( response ) {
			$('centerArea').update( 'Unknown error.' ).show();
		}
	});	
}



/**
 * Validates data coming from the login form and logs the user in.
 */
function logIn() {
	
	//validating
	var matrikkel = $('matrikkel').value;
	var password = hex_md5($('password').value);
	
	if( matrikkel == '' ) {
		$('userError').update( 'Sisesta matrikkel' ).show();
		return;
	}
	if( password == '' ) {
		$('userError').update( 'Sisesta parool' ).show();
		return;
	}
	
	// update the login area
	new Ajax.Request( siteUrl + 'users.php?action=logIn&matrikkel=' + matrikkel + '&password=' + password, {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
			
				var text = response.responseText;
				if( text == 'Error' ) {
					
					$('userError').update( 'Matrikkel ja parool ei klapi' ).show();
					
				} else if( text == 'Deaktiveeritud' ) {
					$('userError').update( 'Konto õppejõu poolt lukustatud' ).show();
				} else {
					$('userError').update().hide();
					$('userLink').update().hide();
					$('userLinks').update( text );
					$('userAreaHeader').hide();
					$('userArea').hide();
					
				
					//update left menu
					new Ajax.Request( siteUrl + 'modules.php?action=usermenu', {
						onSuccess: function( response ) {
							if(response.responseText == "LoggedOut"){
								resetPage();
							} else {
								$('leftArea').update( response.responseText );
							}
						}
					});
					
					// load admin menu (if the user is a teacher)
					new Ajax.Request( siteUrl + 'modules.php?action=adminOptions', {
						onSuccess: function( response ) {
							if(response.responseText == "LoggedOut"){
								resetPage();
							} else {
								$('adminArea').update( response.responseText );
							}
						}
					});
				
				}
			}
		}
	});
	


	
	
}


/**
 * Logs the user out.
 */
function logOut() {
	new Ajax.Request( siteUrl + 'users.php?action=logOut', {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				
				$('userLinks').update( response.responseText );
				$('userAreaHeader').update().hide();
				$('userArea').update().hide();
				$('centerArea').update('');
				$('leftArea').update('');
				$('adminArea').update('');
			}
		}
	});
}

/**
 * Gets the register form from the server and displays it.
 */
function getRegisterArea() {
	$('userAreaHeader').update( 'Registreeri' ).show();
	
	new Ajax.Request( siteUrl + 'users.php?action=getRegisterArea', {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('userArea').update( response.responseText ).show();
			}
		},
		onFailure: function( response ) {
			$('userArea').update( 'Unknown error.' ).show();
		}
	});
}

/**
 * Validates data coming from the register form and registeres the user.
 */
function register() {
	var matrikkel = $('matrikkel').value;
	var password1 = hex_md5($('password1').value);
	var password2 = hex_md5($('password2').value);
	var eesnimi = $('firstName').value;
	var perenimi = $('lastName').value;
	var ruhm = $('group').value;
	
	if( matrikkel == '' ) {
		$('userError').update( 'Sisesta matrikkel' ).show();
		return;
	}

	if( password1 == '' ) {
		$('userError').update( 'Sisesta parool' ).show();
		return;
	}
	if( password2 == '' ) {
		$('userError').update( 'Sisesta parool teist korda' ).show();
		return;
	}
	if( $('password1').value.length < 6 ) {
		$('userError').update( 'Parool peab olema vähemalt 6 tähemärki pikk' ).show();
		return;
	}
	if( password2 == '' ) {
		$('userError').update( 'Sisesta eesnimi' ).show();
		return;
	}
	if( password2 == '' ) {
		$('userError').update( 'Sisesta perenimi' ).show();
		return;
	}
	
	if( password1 != password2 ) {
		$('userError').update( 'Paroolid ei klapi' ).show();
		$('password1').value = '';
		$('password2').value = '';
		return;
	}
	
	new Ajax.Request( siteUrl + 'users.php', {
			parameters: {action: 'register', matrikkel: matrikkel,
				password: password1, eesnimi: eesnimi, perenimi: perenimi,
				ruhm: ruhm},
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				var text = response.responseText;
				if( text != 'Error' ) {
					alert(text);
					$('userError').update().hide();
					getLoginArea();
				} else {
					$('userError').update( 'Selline kasutaja juba eksisteerib' ).show();
				}	
			}
		}
	});
}


/**
 * Changes the password of a user.
 */
function changePassword(id) {
	var id = $('userId').value;
	var password1 = $('password1').value;
	var password2 = $('password2').value;
	
	if( password1 == '' ) {
		$('pwError').update( 'Sisesta uus parool' ).show();
		return;
	}
	
	if( password1.length < 6 ) {
		$('pwError').update( 'Parool peab olema vähemalt 6 tähemärki pikk' ).show();
		return;
	}
	
	if( password2 == '' ) {
		$('pwError').update( 'Sisesta uus parool uuesti' ).show();
		return;
	}
	if( password1 != password2 ) {
		$('pwError').update( 'Sisestatud paroolid ei ole samad' ).show();
		$('password1').value = '';
		$('password2').value = '';
		return;
	}
	
	password1 = hex_md5(password1);
	
	new Ajax.Request( siteUrl + 'users.php', {
		parameters:{action: 'changePassword', password: password1, userId: id},
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				var text = response.responseText;
				if(text == 'OK'){
					$('pwError').update( 'Parool muudetud').show();
				} else {
					$('pwError').update( 'Viga. Proovi hiljem uuesti').show();
				}
			}
		}
	});
}

function getUserData(id){
	new Ajax.Request( siteUrl + 'modules.php?action=getUserData&user=' + id, {
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				var text = response.responseText;
				if( text != 'Error' ) {
					$('centerArea').update( response.responseText );
				}
			}
		}
	});
}

function editUserData() {
	var id = $('userId').value;
	var matrikkel = removeSpaces($('matrikkel').value);
	var eesnimi = $('firstName').value;
	var perenimi = $('lastName').value;
	var ruhm = $('group').value;

	
	if( matrikkel == '' ) {
		alert("Sisesta matrikkel");
		return;
	}
	new Ajax.Request( siteUrl + 'users.php', {
		parameters: {action: 'editUserData', idkasutaja: id,
			matrikkel: matrikkel, eesnimi: eesnimi,
			perenimi: perenimi,
			ruhm: ruhm},		
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				var text = response.responseText;
				alert(text);
				}
			},
		onFailure: function( response ) {
			alert("Andmeid ei saanud uuendada. Proovi hiljem uuesti.");
		}
	});
}

function deleteUser() {
	var id = $('userId').value;
	
	var r=confirm("Kustuta kasutaja?");
	
	if(r){
		new Ajax.Request( siteUrl + 'users.php', {
			parameters: {action: 'deleteUser', idkasutaja: id},		
			onSuccess: function( response ) {
				if(response.responseText == "LoggedOut"){
					resetPage();
				} else {
					var text = response.responseText;
					if(text=="OK"){
						alert("Kasutaja kustutatud");
						loadAllUsers();
					} else alert("Viga. Ei saanud kasutajat kustutada. Proovi hiljem uuesti");
				}},
			onFailure: function( response ) {
				alert("Andmeid ei saanud uuendada. Proovi hiljem uuesti.");
			}
		});
	}
	
	
}


function deActivateUser() {
	var id = $('userId').value;
	new Ajax.Request( siteUrl + 'users.php', {
		parameters: {action: 'deActivateUser', idkasutaja: id},		
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				alert(response.responseText);
				loadAllUsers();
				}
			},
		onFailure: function( response ) {
			alert("Viga. Proovi hiljem uuesti");
		}
	});
	getUserData(id);
}

function activateUser() {
	var id = $('userId').value;
	new Ajax.Request( siteUrl + 'users.php', {
		parameters: {action: 'activateUser', idkasutaja: id},		
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				alert(response.responseText);
				loadAllUsers();
			}
			},
		onFailure: function( response ) {
			alert("Viga. Proovi hiljem uuesti");
		}
	});
	getUserData(id);
}

function getPasswordEditingArea() {
	var id = $('userId').value;
	new Ajax.Request( siteUrl + 'users.php?action=getPasswordArea&userId='+id, {		
		onSuccess: function( response ) {
			if(response.responseText == "LoggedOut"){
				resetPage();
			} else {
				$('passwordEditor').update(response.responseText).show();
			}
			},
		onFailure: function( response ) {
			alert("Viga. Proovi hiljem uuesti");
		}
	});
}

